﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MqttAnalyze.Data.Common
{
    public class ConvertHelper
    {
        public static int GetIntegerId(string strId)
        {
            int id = 0;
            try
            {
                string result = System.Text.RegularExpressions.Regex.Replace(strId ?? "", @"[^(-?\d+(.\d)*)]+", "");
                if (double.TryParse(result, out double dResult))
                {
                    id = Convert.ToInt32(dResult);
                }
            }
            catch
            {

            }
            return id;
        }

        /// <summary>
        /// Int32数字字符串转数字（NULL,空值返回0）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Int32 ToInt32(object str, int defValue = 0)
        {
            Int32 out_data = defValue;
            if (str != null)
            {
                if (!Int32.TryParse(str.ToString().Trim(), out out_data))
                {
                    out_data = defValue;
                }
            }
            return out_data;
        }
        /// <summary>
        /// Double数字字符串转数字（NULL,空值返回0）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Double ToDouble(object str)
        {
            Double out_data = 0;
            if (str != null)
            {
                if (!Double.TryParse(str.ToString().Trim(), out out_data))
                {
                    out_data = 0;
                }
            }
            return out_data;
        }
        /// <summary>
        /// Decimal数字字符串转数字（NULL,空值返回0）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Decimal ToDecimal(object str)
        {
            Decimal out_data = 0;
            if (str != null)
            {
                if (!Decimal.TryParse(str.ToString().Trim(), out out_data))
                {
                    out_data = 0;
                }
            }
            return out_data;
        }

    }
}
